public class PrimeNumbers {
    public static void main(String[] args) {
        long startTime = System.currentTimeMillis();
        
        int count = 0;
        // 2是最小的素数
        for (int i = 2; i <= 20000; i++) {
            if (isPrime(i)) {
                System.out.printf("%-6d", i);
                count++;
                if (count % 5 == 0) {
                    System.out.println();
                }
            }
        }
        
        long endTime = System.currentTimeMillis();
        System.out.println("\n\n程序运行时间：" + (endTime - startTime) + "毫秒");
    }
    
    private static boolean isPrime(int num) {
        if (num <= 1) {
            return false;
        }
        if (num == 2) {
            return true;
        }
        if (num % 2 == 0) {
            return false;
        }
        for (int i = 3; i <= Math.sqrt(num); i += 2) {
            if (num % i == 0) {
                return false;
            }
        }
        return true;
    }
}
